import type { DefaultWalletOptions, Wallet } from '../../../types/Wallet'
import { getInjectedConnector, hasInjectedProvider } from '../../getInjectedConnector'
import { getWalletConnectConnector } from '../../getWalletConnectConnector'
import { roninWalletIcon } from './roninWalletIcon'

export type RoninWalletOptions = DefaultWalletOptions

export const roninWallet = ({ projectId, walletConnectParameters }: RoninWalletOptions): Wallet => {
  const isRoninInjected = hasInjectedProvider({
    namespace: 'ronin.provider'
  })

  const shouldUseWalletConnect = !isRoninInjected

  const getUri = (uri: string) => {
    return `roninwallet://wc?uri=${encodeURIComponent(uri)}`
  }

  return {
    id: 'ronin',
    name: 'Ronin Wallet',
    iconUrl: roninWalletIcon,
    iconBackground: '#ffffff',
    rdns: 'com.roninchain.wallet',
    installed: isRoninInjected || undefined,
    downloadUrls: {
      android: 'https://play.google.com/store/apps/details?id=com.skymavis.genesis',
      ios: 'https://apps.apple.com/us/app/ronin-wallet/id1592675001',
      mobile: 'https://wallet.roninchain.com',
      chrome: 'https://chrome.google.com/webstore/detail/ronin-wallet/fnjhmkhhmkbjkkabndcnnogagogbneec',
      edge: 'https://microsoftedge.microsoft.com/addons/detail/ronin-wallet/kjmoohlgokccodicjjfebfomlbljgfhk',
      firefox: 'https://addons.mozilla.org/firefox/addon/ronin-wallet',
      browserExtension: 'https://wallet.roninchain.com/',
      qrCode: 'https://wallet.roninchain.com/'
    },
    mobile: {
      getUri: shouldUseWalletConnect ? getUri : undefined
    },
    qrCode: shouldUseWalletConnect
      ? {
          getUri: (uri: string) => uri,
          instructions: {
            learnMoreUrl: 'https://wallet.roninchain.com/',
            steps: [
              {
                description: 'We recommend putting Ronin Wallet on your home screen for quicker access.',
                step: 'install',
                title: 'Open the Ronin Wallet app'
              },
              {
                description:
                  'Be sure to back up your wallet using a secure method. Never share your secret phrase with anyone.',
                step: 'create',
                title: 'Create or Import a Wallet'
              },
              {
                description: 'After you scan, a connection prompt will appear for you to connect your wallet.',
                step: 'scan',
                title: 'Tap the scan button'
              }
            ]
          }
        }
      : undefined,
    extension: {
      instructions: {
        learnMoreUrl: 'https://wallet.roninchain.com/',
        steps: [
          {
            description: 'We recommend pinning Ronin Wallet to your taskbar for quicker access to your wallet.',
            step: 'install',
            title: 'Install the Ronin Wallet extension'
          },
          {
            description:
              'Be sure to back up your wallet using a secure method. Never share your secret phrase with anyone.',
            step: 'create',
            title: 'Create or Import a Wallet'
          },
          {
            description: 'Once you set up your wallet, click below to refresh the browser and load up the extension.',
            step: 'refresh',
            title: 'Refresh your browser'
          }
        ]
      }
    },
    createConnector: shouldUseWalletConnect
      ? getWalletConnectConnector({
          projectId,
          walletConnectParameters
        })
      : getInjectedConnector({ namespace: 'ronin.provider' })
  }
}
